package com.yw.atp.model;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yw.atp.Constant;
import com.yw.atp.util.DateUtil;


@Entity
@Table(name=Constant.DB_PREFIX+"reply")
public class Reply implements IDEntity{
	private static final long serialVersionUID = 1L;

	@Id
    @GeneratedValue
    private Integer id;
	private String content;
	@ManyToOne
	private Staff owner;
	@OrderBy("createdAt desc")
	private Date createdAt;
	@ManyToOne
	private Command command;
	private String status = "0";
	private Date updateAt;
	public Date getUpdateAt() {
		return updateAt;
	}

	public void setUpdateAt(Date updateAt) {
		this.updateAt = updateAt;
	}
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	public Command getCommand() {
		return command;
	}

	public void setCommand(Command command){
		this.command = command;
	}

	public Date getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}
	
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Staff getOwner() {
		return owner;
	}

	public void setOwner(Staff owner) {
		this.owner = owner;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	public static JSONArray capture(List<Reply> appms) throws JSONException{
		JSONArray jsonAppms = new JSONArray();
		for(Reply appm : appms){
			jsonAppms.put(appm.capture());
		}
		return jsonAppms;
	}
	
	public JSONObject capture() throws JSONException{
		JSONObject appm = new JSONObject();
		appm.put("id",id);
		appm.put("content",content);
		appm.put("createAt",createdAt!=null?DateUtil.getClientFormatA(createdAt):" ");
		if(owner!=null){
			appm.put("owner",owner.capture());
		}
		if(command!=null){
			appm.put("command",command.capture());
		}
		return appm;
	}
}
